package com.zdpx.pxframework.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdpx.pxframework.core.validator.group.AddGroup;
import com.zdpx.pxframework.core.validator.group.UpdateGroup;
import com.zdpx.pxframework.core.vo.PageResult;
import com.zdpx.pxframework.core.vo.Result;
import com.zdpx.pxframework.system.api.UserRoleControllerApi;
import com.zdpx.pxframework.system.entity.UserRole;
import com.zdpx.pxframework.system.service.UserRoleService;
import com.zdpx.pxframework.system.vo.query.UserRoleQuery;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * 用户角色关联 接口控制器
 *
 * @Description:
 * @Author: Cheng XiaoXiao
 * @Date: 2022-02-27 15:20:14
 */
@RestController
@RequestMapping("/user-role")
public class UserRoleController implements UserRoleControllerApi {
    @Resource
    private UserRoleService userRoleService;


    /**
     * 获取条件查询信息
     *
     * @param userRoleQuery 查询条件
     * @return 查询封装
     */
    private LambdaQueryWrapper<UserRole> getUserRoleQueryWrapper(UserRoleQuery userRoleQuery) {
        LambdaQueryWrapper<UserRole> wrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(userRoleQuery.getUserId())) {
            wrapper.eq(UserRole::getUserId, userRoleQuery.getUserId());
        }
        if (!StringUtils.isEmpty(userRoleQuery.getRoleId())) {
            wrapper.eq(UserRole::getRoleId, userRoleQuery.getRoleId());
        }
        return wrapper;
    }

    /**
     * 按照条件查询用户角色关联信息
     *
     * @param userRoleQuery 查询条件
     * @return 条件结果信息
     */
    @Override
    @GetMapping
    public Result<List<UserRole>> list(UserRoleQuery userRoleQuery) {
        return Result.success(userRoleService.list(getUserRoleQueryWrapper(userRoleQuery)));
    }

    /**
     * 分页及条件查询用户角色关联信息
     *
     * @param page          页码
     * @param size          分页大小
     * @param userRoleQuery 查询条件
     * @return 分页结果信息
     */
    @Override
    @GetMapping("/{size}/{page}")
    public Result<PageResult<UserRole>> search(@PathVariable("size") Long size, @PathVariable("page") Long page, UserRoleQuery userRoleQuery) {
        if (page <= 0) {
            page = 1L;
        }
        if (size <= 0) {
            size = 10L;
        }
        Page<UserRole> pageParam = new Page<>(page, size);

        userRoleService.page(pageParam, getUserRoleQueryWrapper(userRoleQuery));
        PageResult<UserRole> pageResult = new PageResult<UserRole>(pageParam.getTotal(), pageParam.getSize(), pageParam.getCurrent(), pageParam.getRecords());

        return Result.success(pageResult);
    }

    /**
     * 根据ID获取用户角色关联详情
     *
     * @param id 用户角色关联ID
     * @return 用户角色关联详细信息
     */
    @Override
    @GetMapping("/{id}")
    public Result<UserRole> detail(@PathVariable("id") String id) {
        return Result.success(userRoleService.detailById(id));
    }

    /**
     * 增加用户角色关联信息
     *
     * @param userRole 用户角色关联增加实体
     * @return 增加后的用户角色关联信息
     */
    @Override
    @PostMapping
    public Result<UserRole> add(@RequestBody @Validated(AddGroup.class) UserRole userRole) {
        userRoleService.saveUserRole(userRole);
        return Result.success(userRole);
    }

    /**
     * 修改用户角色关联信息
     *
     * @param id       用户角色关联ID
     * @param userRole 用户角色关联修改实体
     * @return 修改后的用户角色关联信息
     */
    @Override
    @PutMapping("/{id}")
    public Result<UserRole> updateById(@PathVariable("id") String id, @RequestBody @Validated(UpdateGroup.class) UserRole userRole) {
        userRole.setId(id);
        userRoleService.updateUserRoleById(userRole);
        return Result.success(userRoleService.getById(id));
    }


    /**
     * 删除用户角色关联信息
     *
     * @param id 用户角色关联ID
     * @return 删除结果
     */
    @Override
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteById(@PathVariable("id") String id) {
        userRoleService.deleteById(id);
        return Result.success(true);
    }

    /**
     * 删除用户角色关联信息
     *
     * @param idList 用户角色关联ID数组
     * @return 删除结果
     */
    @Override
    @DeleteMapping("/batch-remove")
    public Result<Boolean> batchRemove(@RequestBody List<String> idList) {
        userRoleService.removeByIds(idList);
        return Result.success(true);
    }
}
